package com.chenzhou.dao;

import com.chenzhou.common.BaseDAO;
import com.chenzhou.entity.GoodsImage;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class GoodsImageDAOImpl extends BaseDAO<GoodsImage> {

    @Override
    protected List<GoodsImage> resultToList(ResultSet rs) throws SQLException {
        List<GoodsImage> list = new ArrayList<>();
        while(rs.next()){

            GoodsImage goodsImage = new GoodsImage();
            goodsImage.setId(rs.getInt("id"));
            goodsImage.setFilename(rs.getString("filename"));
            goodsImage.setFile_url(rs.getString("file_url"));
            goodsImage.setFile_pix(rs.getString("file_pix"));
            goodsImage.setGood_id(rs.getInt("good_id"));
            goodsImage.setMarkup(rs.getString("markup"));
            list.add(goodsImage);
        }
        return list;
    }

    public List<GoodsImage> queryGoodImageByGoodsID(Integer id){
        String sql = "select * from tbl_images where good_id=?";
        Object[] objects = new Object[]{id};
        return this.execute_Query(sql,objects);
    }

    public GoodsImage queryqueryGoodImageByImageID(Integer image_id){
        String sql = "select * from tbl_images where id=?";
        Object[] objects = new Object[]{image_id};
        List<GoodsImage> list = this.execute_Query(sql,objects);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    /**
     * 上传图片
     * @param goodsImage
     * @return
     */
    public int add_GoodIamge(GoodsImage goodsImage){
        String sql = "insert into tbl_images values(null,?,?,?,?,?)";
        Object[] objects = new Object[]{
                goodsImage.getFilename(),
                goodsImage.getFile_url(),
                goodsImage.getFile_pix(),
                goodsImage.getGood_id(),
                goodsImage.getMarkup()
        };
        return this.execute_Modify(sql,objects);
    }

    /***
     * 删除图片
     * @param id
     * @return
     */
    public int delete_goodsImage(Integer id){
        String sql = "delete from tbl_images where id = ?";
        Object[] objects = new Object[]{id};
        return this.execute_Modify(sql,objects);
    }

}
